package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MerchantFeedback;
import com.ruoyi.system.service.IMerchantFeedbackService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户反馈 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-11
 */
@Controller
@RequestMapping("/system/merchantFeedback")
public class MerchantFeedbackController extends BaseController
{
    private String prefix = "system/merchantFeedback";
	
	@Autowired
	private IMerchantFeedbackService merchantFeedbackService;
	
	@RequiresPermissions("system:merchantFeedback:view")
	@GetMapping()
	public String merchantFeedback()
	{
	    return prefix + "/merchantFeedback";
	}
	
	/**
	 * 跳转物料图片页面
	 * @return
	 */
	@RequiresPermissions("system:merchantFeedback:view")
	@GetMapping("/materialImg")
	public String materialImg()
	{
	    return prefix + "/materialImg";
	}
	
	/**
	 * 查询用户反馈列表
	 */
	@RequiresPermissions("system:merchantFeedback:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MerchantFeedback merchantFeedback)
	{
		startPage();
        List<MerchantFeedback> list = merchantFeedbackService.selectMerchantFeedbackList(merchantFeedback);
		return getDataTable(list);
	}
	/**
	 * 导出用户反馈列表
	 */
	@RequiresPermissions("system:merchantFeedback:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerchantFeedback merchantFeedback)
    {
    	List<MerchantFeedback> list = merchantFeedbackService.selectMerchantFeedbackList(merchantFeedback);
        ExcelUtil<MerchantFeedback> util = new ExcelUtil<MerchantFeedback>(MerchantFeedback.class);
        return util.exportExcel(list, "merchantFeedback");
    }
	
	/**
	 * 新增用户反馈
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户反馈
	 */
	@RequiresPermissions("system:merchantFeedback:add")
	@Log(title = "用户反馈", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MerchantFeedback merchantFeedback)
	{		
		merchantFeedback.setId(StringUtils.randomUUID());
		merchantFeedback.setCreateTime(new Date());
		return toAjax(merchantFeedbackService.insertMerchantFeedback(merchantFeedback));
	}

	/**
	 * 修改用户反馈
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		MerchantFeedback merchantFeedback = merchantFeedbackService.selectMerchantFeedbackById(id);
		mmap.put("merchantFeedback", merchantFeedback);
	    return prefix + "/edit";
	}
	
	/**
	 * 客服回复
	 */
	@RequiresPermissions("system:merchantFeedback:edit")
	@Log(title = "用户反馈", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MerchantFeedback merchantFeedback)
	{	
		merchantFeedback.setReplyUser(getSysUser().getUserName());
		merchantFeedback.setStatus("1");//已回复
		return toAjax(merchantFeedbackService.updateMerchantFeedback(merchantFeedback));
	}
	
	/**
	 * 删除用户反馈
	 */
	@RequiresPermissions("system:merchantFeedback:remove")
	@Log(title = "用户反馈", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(merchantFeedbackService.deleteMerchantFeedbackByIds(ids));
	}
	
	/**
	 * 查询物料图片列表
	 *
	 */
	@RequiresPermissions("system:merchantFeedback:list")
	@PostMapping("/materialList")
	@ResponseBody
	public TableDataInfo materialList(MerchantFeedback merchantFeedback)
	{
		startPage();
		merchantFeedback.setMerchantId("1");//物料列表
        List<MerchantFeedback> list = merchantFeedbackService.selectMerchantFeedbackList(merchantFeedback);
		return getDataTable(list);
	}
	
	/**
	 * 上传物流图片
	 * @throws IOException 
	 * @throws FileUploadException 
	 */
	@PostMapping("/uploadMaterial")
	@ResponseBody
	public Map<String,Object> uploadMaterial(@RequestParam("file") MultipartFile multipartFile,HttpServletRequest request,HttpServletResponse response) throws FileUploadException, IOException {
		Map<String, Object> uploadByAliyun = FileUploadUtils.uploadByAliyun(multipartFile, "material");
		return uploadByAliyun;
	}
	 /**
     * 物料状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(MerchantFeedback merchantFeedback)
    {
        return toAjax(merchantFeedbackService.updateMerchantFeedback(merchantFeedback));
    }
}
